clear 
format short g

%
% This program estimates the COMPETITIVE stationary case, minimizing price and sales
% moments.  Each product is a firm, firms compete in prices, and there is
% general obsolescence.
%
% There are a set of flexible parameters and a set of fixed parameters
% BETA distribution of income
%
%
% Note: 11/24 nashN_inv.m gives same results as nashN.m
% using nashN.m, use alternative starting price vector in nashN.m
% resulted in same prices.
%
% Documented on 3/2015 for RESTAT 
%

global tol1 tol2 flg_otpt par_disct beta_est

%
% Load in parameters for price function
% (coefficients estimated using NPD data)
%
% this should be a third order polynominal
load prc_par_estimates3
beta_est = res_ols3.beta;
disp('Price decline coefficient estimates');
disp(beta_est);
clear res_ols3

% Settig values for fixed parameters values and choosing initial
% starting values for flexible parameters
% Note: initial values for flexible are redefined below

% range of income distribution [a, a+b]
   a = 1;    
   b = 15;   
% total mass of consumers   
   mrkt_size = 1;  
% utility level growth rate 
%   g_rte = 1.017;  % from data -- CPU growth rate
   g_rte = 1.029;  % Moore's law
   
% utility of outside good 
nu_low = 0.33;
%nu_low = 2.0;

% utility level of best computer
   nu_level = 10;
% marginal cost (same for all firms)
   mc_level = 1;
% discount rate
   par_disct = 0.99;
    
% tolerance for fminsearch
    options = optimset('TolFun',1e-9);
% tolerance for nash equil
    tol1=1e-8;
% tolerance for bisection routine of FONC
    tol2=1e-8;   
% specify the rank of the highest vintage 
    N = 10;         
% the number of initial competitors
% don't set this less than 16
    nmbr_cmpt = 20;  

% Setting fixed parameters
    par_set = [N,nmbr_cmpt,nu_level,a,nu_low,g_rte];

% Choosing flexible parameters 
% par_beta1 and par_beta2 are the two parameters which
% characterize the Beta distribution
%
%    int_vec = [b,mc_level, par_beta1, par_beta2];

int_vec=[8.94861; 0.872581; 0.697482; 1.71831];
    
% =1 if want to print out moments
flg_otpt = 0;
est_par = fminsearch(@(x) cmpt_gmm_alt(x,par_set),int_vec); 

% How par_vec is defined depends upon whether fminsearch is called
%   par_vec = int_vec;   
   par_vec = est_par;
    
    disp('Estimated parameters (flexible and fixed)');
    
% Converting parameters
labels = ['Frontier value        ';
          'Quality growth rate   ';
          'Utility ratio         ';
          'income lower bound    ';
          'income upper bound    ';
          'Density of consumer1  ';
          'Density of consumer2  ';
          'marginal cost         '];
      
par_cnvt = zeros(8,1);
par_cnvt(1) = par_set(1);
par_cnvt(2) = par_set(6);
par_cnvt(3) = par_set(5)/par_set(3);
par_cnvt(4) = par_set(4);
par_cnvt(5) = par_vec(1)+par_set(4);
par_cnvt(6) = par_vec(3);
par_cnvt(7) = par_vec(4);
par_cnvt(8) = par_vec(2);

disp([labels num2str(par_cnvt)]);
disp(' ');
    
% Displaying moments
    flg_otpt=1;
    cmpt_gmm_alt(par_vec,par_set);
    flg_otpt=0;    
    
    
disp('Thats it');    

